//
//  ResidentDataService.m
//  homeland
//
//  Created by Alawn on 13-10-31.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "ResidentDataService.h"
#import "UserInfoService.h"
#import "NSObject+propertyList.h"
@implementation ResidentDataService
//信件提交方法
-(void)postComplainWithDataItem:(ResidentDataItem *)item
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:item.userName forKey:@"XFUserName"];
    [params setValue:@"男" forKey:@"Sex"];
    [params setValue:item.Phone forKey:@"Phone"];
    [params setValue:@"其他" forKey:@"PaperType"];
    [params setValue:@"123456" forKey:@"PaperCode"];
    [params setValue:item.address forKey:@"UAddress"];
    [params setValue:item.title forKey:@"Subject"];
    [params setValue:@"其他" forKey:@"SubjectType"];
    [params setValue:item.content forKey:@"Content"];
    [params setValue:@"4" forKey:@"PropertiesType"];
    [params setValue:@"海珠区" forKey:@"Town"];
    [params setValue:item.happenStreet forKey:@"Street"];
    [params setValue:item.happenAddress forKey:@"Address"];
    [params setValue:@"是" forKey:@"IsOpen"];
    [params setValue:@"" forKey:@"Memo"];
    [params setValue:@"无" forKey:@"PetitionLetterState"];
    
    [self doHttpPost:POST_ResidentComplain_URL headers:nil path:nil parameters:params];
}
-(void)loadDataWithNoAndName:(NSString *)keySLIDOrNil withName:(NSString*)keyName
{
    _currentPageNo = 0;//刷新或查询的时候将请求页码变为0
    if (self.dataList) {
        self.dataList = nil;
    }
    [self startRequestWithNoName:keySLIDOrNil withName:keyName];
}

-(void)loadMoreDataWithNoAndName:(NSString *)keySLIDOrNil withName:(NSString*)keyName
{
    [self startRequestWithNoName:keySLIDOrNil withName:keyName];
}

-(void)startRequestWithNoName:(NSString *)keySLIDOrNil withName:(NSString*)keyName
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (keySLIDOrNil) {
        [params setValue:keySLIDOrNil forKey:@"SLID"];
    } else {
        [params setValue:@"" forKey:@"SLID"];//X131102003-admin
    }
    if (keyName) {
        [params setValue:keyName forKey:@"XFUserName"];
    } else {
        [params setValue:@"" forKey:@"XFUserName"];//X131102003-admin
    }
//    [params setValue:@"叶文威" forKey:@"XFUserName"];//X131102003-admin
//    [params setValue:@"叶文威" forKey:@"XFUserName"];//X130604008
//    [params setValue:[UserInfoService getUserInfoService].userInfo.userName forKey:@"XFUserName"];
    _dataItemName = @"ResidentDataItem";
    [self doHttpPost:GetNewXFinfo_URL headers:nil path:@"" parameters:params];
}
-(void)doHttpPost:(NSString *)posturl headers:(NSDictionary*)headersOrNil path:(NSString*)path parameters:(NSDictionary *)params
{
    UserInfoService *userInfo=[UserInfoService getUserInfoService];
    if ([params objectForKey:@"userId"]==nil) {
       [params setValue:userInfo.userInfo.Id forKey:@"userId"];
    }
    
    MKNetworkEngine *currentEngine = [[MKNetworkEngine alloc] initWithHostName:posturl customHeaderFields:headersOrNil];
    MKNetworkOperation *op = [currentEngine operationWithPath:path params:params httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *operation) {
        
        BOOL isSuccess = NO;
           NSDictionary *json = [operation responseJSON];
        if ([json objectForKey:@"replyOpinion"]) {
            isSuccess = YES;//replyOpinion
            ResidentDataItem *tempItem=[[ResidentDataItem alloc] init];
            NSArray *properties = [tempItem getPropertyList];
            for (NSString *propertyName in properties) {
                NSString *key = propertyName;
                if ([propertyName isEqualToString:@"Id"]) {
                    key = @"id";//因为属性不能叫做id所以把I变成了大写
                }
                [tempItem setValue:[json objectForKey:key] forKey:propertyName];
            }
            NSMutableArray *tempArray = [NSMutableArray array];
            [tempArray addObject:tempItem];
            self.dataList=nil;
            self.dataList=tempArray;
            
        }
        else if ([json objectForKey:@"isSuccess"])
        {
            isSuccess=YES;
        }
        else {
            self.dealCode = [json objectForKey:@"slid"];
            isSuccess = ((NSNumber*)[json objectForKey:@"result"]).boolValue;
        }
        //执行回调
        if (isSuccess) {
            if (self.delegate && [self.delegate respondsToSelector:@selector(loadDataFinished)]) {
                [self.delegate loadDataFinished];
            }
        } else {
            if (self.delegate && [self.delegate respondsToSelector:@selector(loadDataFaild:)]) {
                [self.delegate loadDataFaild:@"提交失败"];
                 self.dataList=nil;
            }
        }
        
    }errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        NSLog(@"MKNetwork request error : %@", [err localizedDescription]);
        if (self.delegate && [self.delegate respondsToSelector:@selector(loadDataFaild:)]) {
            [self.delegate loadDataFaild:@"服务器网络异常"];
        }
    }];
    [currentEngine enqueueOperation:op];
}
-(void)voteForReservationWithID:(NSString *)ID AndVote:(NSString *)vote
{
    UserInfoService *userInfo=[UserInfoService getUserInfoService];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:ID forKey:@"SLID"];
    [params setValue:userInfo.userInfo.Id forKey:@"USER_ID"];
    [params setValue:userInfo.userInfo.Id forKey:@"userId"];
    [params setValue:vote forKey:@"VOTE_GRADE"];
    [self doHttpPost:SERVER_URL_FLOW_VOTE_GRADE headers:nil path:nil parameters:params];
}
@end
